package com.wtwd.wtwddevice.listener.appMsg;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.FrequencyTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.Frequency;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IFrequencyService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * app 设置心率
 */
@MsgType(MsgTypeEnum.APP_SET_W_HRTSTART)
@Component
public class AppSetHeartRateHandler implements IMsgHandler {

    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private PushAppMsgService msgService;

    @Resource
    private IFrequencyService frequencyService;

    @Resource
    private IDeviceService deviceService;


    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        Integer data = (Integer) appProtocol.getData();
        String number = deviceSendService.generateNumber(appProtocol.getImei());
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(MsgTypeEnum.W_HRTSTART.getCmdName())
                .body(String.valueOf(data))
                .number(number)
                .build();
        AppProtocol protocol;
        if (deviceSendService.sendMsgDevice(wtProtocol)) {//如果为fase直接告知app端设置失败
            //插入或者更新心率频率设置值
            Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
            Frequency frequency = new Frequency();
            frequency.setDeviceId(deviceId);
            frequency.setNumber(number);
            frequency.setType(FrequencyTypeEnum.HEARTRATE.getType());
            frequency.setValue(data);
            frequency.setState(CmdStateEnum.SENDING.getState());
            frequencyService.insertOrUpdate(frequency);
            protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_W_HRTSTART.getCmdName())
                    .state(AnswerStateEnum.NORMAL.getState())
                    .data(null)
                    .build();
        } else {
            protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_W_HRTSTART.getCmdName())
                    .state(AnswerStateEnum.ABNORMAL.getState())
                    .data(null)
                    .build();
            msgService.pushToDevice(messageBase.getForm(),
                    MessageBase.builder()
                            .appProtocol(protocol)
                            .build()
            );
        }
    }
}
